import request from "@/utils/request";
import type {
  GetRoleListApiParams,
  UpdateRoleApiParams,
  GetRolePermissionApiResponse,
} from "./model/roleModel";
import type { GetUserListApiResponse } from "./model/userModel";

enum api {
  getRoleList = "/admin/acl/role",
  deleteRole = "/admin/acl/role/remove",
  addRole = "/admin/acl/role/save",
  updateRole = "/admin/acl/role/update",
  getRolePermission = "/admin/acl/permission/toAssign",
  distributePermission = "/admin/acl/permission/doAssign",
}
/**
 * 获取角色分页列表
 * @param  GetRoleListApiParams
 * @returns GetUserListApiResponse
 */
export const getRoleListApi = ({
  page,
  limit,
  roleName,
}: GetRoleListApiParams) => {
  return request.get<any, GetUserListApiResponse>(
    api.getRoleList + `/${page}/${limit}`,
    {
      params: {
        roleName,
      },
    }
  );
};
/**
 * 删除角色的接口
 * @param id 角色id
 * @returns null
 */
export const deleteRoleApi = (id: string) => {
  return request.delete<any, null>(api.deleteRole + `/${id}`);
};
/**
 * 添加角色的接口
 * @param roleName 角色名
 * @returns null
 */
export const addRoleApi = (roleName: string) => {
  return request.post<any, null>(api.addRole, { roleName });
};
/**
 * 修改角色的接口
 * @param UpdateRoleApiParams
 * @returns null
 */
export const updateRoleApi = ({ roleName, id }: UpdateRoleApiParams) => {
  return request.put<any, null>(api.updateRole, {
    roleName,
    id,
  });
};
/**
 * 根据角色获取菜单
 * @param roleId 角色id
 * @returns GetRolePermissionApiResponse
 */
export const getRolePermissionApi = (roleId: string) => {
  return request.get<any, GetRolePermissionApiResponse>(
    api.getRolePermission + `/${roleId}`
  );
};
/**
 * 给角色分配权限
 * @param roleId 角色id
 * @param permissionId 权限数组
 * @returns null
 */
export const distributePermissionApi = (
  roleId: string,
  permissionId: string[]
) => {
  return request.post<any, null>(
    api.distributePermission,
    {},
    {
      params: {
        roleId,
        permissionId,
      },
    }
  );
};
